﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace TeacherHelp.FormList.Teacher
{
    public partial class TeacherMainForm : BaseForm
    {
        #region 子窗体业务处理
        /// <summary>
        /// 题目列表详情窗体
        /// </summary>
        public SubjectsEditForm subjectsEditForm = null;
        public ExercisesListForm exercisesListForm = null;
        public ExercisesAddForm exercisesAddForm = null;
        public ExercisesEditForm exercisesEditForm = null;
        public ExercisesRemoveForm exercisesRemoveForm = null;
        public ImportExercisesForm importExercisesForm = null;
        public StudentAchievement studentAchievement = null;
        //public UserRemoveForm userRemoveForm = null;

        public void ChildFormInt()
        {
            exercisesListForm = new ExercisesListForm(this);
            child_ShowForm(exercisesListForm);
        }

        /// <summary>
        /// 子窗体展示
        /// </summary>
        /// <typeparam name="T">窗体类型</typeparam>
        /// <param name="form">窗体实例对象</param>
        public void child_ShowForm<T>(T form, bool isFill = true) where T : Form
        {
            //this.IsMdiContainer = true;//父窗体
            //panelContent.Dock = DockStyle.Fill;
            contetnPanel.Controls.Clear();

            form.MdiParent = this;
            if (isFill) form.Dock = DockStyle.Fill;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.WindowState = FormWindowState.Normal;
            form.Parent = contetnPanel;

            form.Show();
        }
        #endregion

        public TeacherMainForm()
        {
            InitializeComponent();
            ChildFormInt();
        }

        private void AdminMainForm_Load(object sender, EventArgs e)
        {
            loadTimer.Start();
            timeTimer.Start();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void MinBtn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void MaxBtn_Click(object sender, EventArgs e)
        {
            MaxMinSwitch();
        }

        private void MaxMinSwitch()
        {
            //窗体最大化 最小化
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.MaxBtn.BackgroundImage = global::TeacherHelp.Properties.Resources.fullscreen_expand;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
                this.MaxBtn.BackgroundImage = global::TeacherHelp.Properties.Resources.fullscreen_shrink;
            }
        }

        private void panelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //窗体拖动
                ReleaseCapture();
                SendMessage(Handle, 0xA1, 0x02, 0);
            }
        }

        private void timeTimer_Tick(object sender, EventArgs e)
        {
            this.thisTimeLable.Text = "当前时间：" + DateTime.Now;
        }

        private void loadTimer_Tick(object sender, EventArgs e)
        {
            if (Opacity == 1)
            {
                loadTimer.Stop();
            }
            else
            {
                Opacity += 0.1;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void teacherNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        private void AddExercisesBtn_Click(object sender, EventArgs e)
        {
            exercisesAddForm = new ExercisesAddForm(this);
            child_ShowForm(exercisesAddForm, false);
        }

        private void EditSubjectBtn_Click(object sender, EventArgs e)
        {
            subjectsEditForm = new SubjectsEditForm();
            child_ShowForm(subjectsEditForm, false);
        }

        private void EditExercisesBtn_Click(object sender, EventArgs e)
        {
            ExercisesEdit();
        }

        public void ExercisesEdit()
        {
            var selectRow = exercisesListForm.ExercisesInfoGridView.SelectedRows;
            if (selectRow.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var exercisesId = selectRow[selectRow.Count - 1].Cells[nameof(Model.Exercises.Id)].Value.ToString();
            exercisesEditForm = new ExercisesEditForm(this, exercisesId);
            child_ShowForm(exercisesEditForm, false);
        }

        private void DeleteExercisesBtn_Click(object sender, EventArgs e)
        {
            var selectRow = exercisesListForm.ExercisesInfoGridView.SelectedRows;
            if (selectRow.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var exercisesId = selectRow[0].Cells[nameof(Model.Exercises.Id)].Value.ToString();
            exercisesRemoveForm = new ExercisesRemoveForm(this, exercisesId);
            child_ShowForm(exercisesRemoveForm, false);
        }

        private void ExercisesListBtn_Click(object sender, EventArgs e)
        {
            exercisesListForm = new ExercisesListForm(this);
            child_ShowForm(exercisesListForm);
        }

        private void ImportExercisesBtn_Click(object sender, EventArgs e)
        {
            importExercisesForm = new ImportExercisesForm();
            child_ShowForm(importExercisesForm);
        }

        private void StudentGradeBtn_Click(object sender, EventArgs e)
        {
            studentAchievement = new StudentAchievement();
            child_ShowForm(studentAchievement);
        }
    }
}
